/**
 *
 */

package it.bd.SetAllConnectionsOff.activity;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TabHost;

import it.bd.SetAllConnectionsOff.R;
import it.bd.SetAllConnectionsOff.helper.BluetoothHelper;
import it.bd.SetAllConnectionsOff.helper.GpsHelper;
import it.bd.SetAllConnectionsOff.helper.NetworkHelper;
import it.bd.SetAllConnectionsOff.helper.PowerHelper;
import it.bd.SetAllConnectionsOff.helper.WifiHelper;

/**
 * tab that saves the Services that would be closed when the widget will be launched
 * 
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class LimitedStateActivity extends ATabActivity {
    private Context context;

    public LimitedStateActivity() {
        super();
    }

    public LimitedStateActivity(TabHost tabHost, Context context) {
        super();
        this.context = context;
        attachOnTabHost(tabHost);
    }

    @Override
    String getIndicator() {
        return getContext().getString(R.string.limitedSettings);
    }

    @Override
    public String getTabTag() {
        return "limitedState";
    }

    @Override
    void setupBluetooth() {
        CheckBox c = cbBluetoothState;
        final BluetoothHelper helper = BluetoothHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            Boolean b = helper.getLimitedStateValue();
            c.setChecked(b != null && b);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    helper.setLimitedStateValue(isChecked);
                }
            });
        }
    }

    @Override
    void setupGps() {
        CheckBox c = cbGpsState;
        final GpsHelper helper = GpsHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            Boolean b = helper.getLimitedStateValue();
            c.setChecked(b != null && b);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    helper.setLimitedStateValue(isChecked);
                }
            });
        }
    }

    @Override
    void setupNetwork() {
        CheckBox c = cbNetworkState;
        final NetworkHelper helper = NetworkHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            Boolean b = helper.getLimitedStateValue();
            c.setChecked(b != null && b);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    helper.setLimitedStateValue(isChecked);
                }
            });
        }

    }

    @Override
    void setupScreenBrightness() {
        Integer brightness = PowerHelper.getInstance().getLimitedStateValue();
        if (brightness == null) {
            brightness = -2;
        }
        if (brightness < 0) {
            cbBrightness.setChecked(false);
            sbScreenBrightness.setEnabled(false);
            sbScreenBrightness.setProgress(0);
            tvBrightness.setText("");
        } else {
            cbBrightness.setChecked(true);
            sbScreenBrightness.setEnabled(true);
            sbScreenBrightness.setProgress(brightness);
            tvBrightness.setText("" + brightness);
        }
        cbBrightness.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    PowerHelper.getInstance().setLimitedStateValue(-2);
                    sbScreenBrightness.setEnabled(false);
                    sbScreenBrightness.setProgress(0);
                    tvBrightness.setText("");
                } else {
                    Integer brightness = PowerHelper.getInstance().getLimitedStateValue();
                    if (brightness == null || brightness < 0) {
                        brightness = PowerHelper.getInstance().getScreenBrighteness();
                    }
                    sbScreenBrightness.setEnabled(true);
                    sbScreenBrightness.setProgress(brightness);
                    tvBrightness.setText(" " + brightness);
                }
            }
        });

        sbScreenBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    PowerHelper.getInstance().setLimitedStateValue(progress);
                    tvBrightness.setText("" + progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    @Override
    void setupWifi() {
        CheckBox c = cbWifiState;
        final WifiHelper helper = WifiHelper.getInstance();
        c.setChecked(false);
        c.setEnabled(helper.isServicePresent());
        if (c.isEnabled()) {
            Boolean b = helper.getLimitedStateValue();
            c.setChecked(b != null && b);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    helper.setLimitedStateValue(isChecked);
                }
            });
        }
    }

    @Override
    Context getContext() {
        return context;
    }

}
